package cloud.tianai.ram.policy.spring.autoconfiguration;

import cloud.tianai.ram.policy.core.PermissionHandler;
import cloud.tianai.ram.policy.core.action.ActionHandler;
import cloud.tianai.ram.policy.core.action.AntActionHandler;
import cloud.tianai.ram.policy.core.condition.ConditionHandlerManager;
import cloud.tianai.ram.policy.core.resource.AntResourceHandler;
import cloud.tianai.ram.policy.core.resource.ResourceHandler;
import cloud.tianai.ram.policy.spring.CurrentUserStatementProvider;
import cloud.tianai.ram.policy.spring.ActionHolder;
import cloud.tianai.ram.policy.spring.SpringConditionHandlerManager;
import cloud.tianai.ram.policy.spring.SpringPermissionHandler;
import cloud.tianai.ram.policy.spring.aop.ActionAnnotationAdvisor;
import cloud.tianai.ram.policy.spring.aop.ActionInterceptor;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: 天爱有情
 * @date 2022/3/23 16:43
 * @Description Ram自动装配器
 */
@Configuration
public class RamAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public ConditionHandlerManager conditionHandlerManager(BeanFactory beanFactory) {
        return new SpringConditionHandlerManager(beanFactory);
    }
    @Bean
    @ConditionalOnMissingBean
    public ActionHolder actionHolder() {
        return new ActionHolder();
    }

    @Bean
    @ConditionalOnMissingBean
    public ActionHandler actionHandler() {
        return new AntActionHandler();
    }

    @Bean
    @ConditionalOnMissingBean
    public ResourceHandler resourceHandler() {
        return new AntResourceHandler();
    }

    @Bean
    @ConditionalOnMissingBean
    public PermissionHandler permissionHandler(BeanFactory beanFactory) {
        return new SpringPermissionHandler(beanFactory);
    }

    @Bean
    @ConditionalOnMissingBean
    public ActionInterceptor actionInterceptor(PermissionHandler permissionHandler,
                                               CurrentUserStatementProvider currentUserPermissionProvider,
                                               ActionHolder actionHolder) {
        return new ActionInterceptor(permissionHandler, currentUserPermissionProvider, actionHolder);
    }

    @Bean
    @ConditionalOnMissingBean
    public ActionAnnotationAdvisor actionAnnotationAdvisor(ActionInterceptor actionInterceptor) {
        return new ActionAnnotationAdvisor(actionInterceptor);
    }

}
